package com.tanhua.manage.controller;


import com.tanhua.autoconfig.template.OssTemplate;
import com.tanhua.manage.service.SoulService;
import com.tanhua.model.mongo.TestOption;
import com.tanhua.model.mongo.TestPaper;
import com.tanhua.model.mongo.TestQuestion;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.PaperVo;
import com.tanhua.model.vo.QuestionVo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/soul")
public class SoulController {

    @Autowired
    private SoulService soulService;

    @Autowired
    private OssTemplate ossTemplate;
    //分页查询选项
    @GetMapping("/option/findByPage")
    public ResponseEntity findOptionByPage(@RequestParam(defaultValue = "1") Integer page,
                                           @RequestParam(defaultValue = "10") Integer pageSize){

        PageResult vo=soulService.findOptionByPage(page,pageSize);
        return ResponseEntity.ok(vo);
    }
    //根据id删除选项
    @DeleteMapping("/option/delete")
    public ResponseEntity deleteOption(String id){

        Boolean vo=soulService.deletOption(id);
        return ResponseEntity.ok(vo);
    }

    //根据id查询选项
    @GetMapping("/option/findById")
    public ResponseEntity findOptionById(String id){
        TestOption option=soulService.findOptionById(id);
        return ResponseEntity.ok(option);
    }

    //保存或更新
    @PostMapping("/option/saveOrUpdate")
    public ResponseEntity saveOrUpdateOption(@RequestBody TestOption options,String optionId){
        try {

            if (options.getId()==null){
                soulService.saveOption(options);

            }else {
                soulService.updateOption(options,optionId);

            }
            return ResponseEntity.ok(true);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(false);
        }
    }

    //查询所有问题
    @GetMapping("/questions/findAll")
    public ResponseEntity findAllQuestions(){
        List<QuestionVo> list=soulService.findAllQuestions();
        return ResponseEntity.ok(list);
    }
    //分页查询问题
    @GetMapping("/questions/findByPage")
    public ResponseEntity findQuestionsByPage(@RequestParam(defaultValue = "1") Integer page,
                                           @RequestParam(defaultValue = "10") Integer pageSize){

        PageResult vo=soulService.findQuestionsByPage(page,pageSize);
        return ResponseEntity.ok(vo);
    }
    //根据id删除问题
    @DeleteMapping("/questions/delete")
    public ResponseEntity deleteQuestion(String id){
        try {

            soulService.deleteQuestion(id);
            return ResponseEntity.ok(true);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(false);
        }

    }
    //根据id查询问题
    @GetMapping("/questions/findById")
    public ResponseEntity findQuestionById(String id){
        TestQuestion question=soulService.findQuestionById(id);
        return ResponseEntity.ok(question);
    }
    //保存和修改问题
    @PostMapping("/questions/saveOrUpdate")
    public ResponseEntity saveOrUpdateQuestion(@RequestBody TestQuestion questions,String questionId){
        try {

            if (questions.getId()==null){
                soulService.saveQuestions(questions);

            }else {
                soulService.updateQuestions(questions,questionId);

            }
            return ResponseEntity.ok(true);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(false);
        }
    }

    //查询所有试卷
    @GetMapping("/papers/findAll")
    public ResponseEntity findAllPapers(){
        List<PaperVo> list=soulService.findAllPapers();
        return ResponseEntity.ok(list);
    }

    //根据试卷id查询试卷内容
    @GetMapping("/paper/findContentById")
    public ResponseEntity findPaperContentByPaperId(String id){
        Map<String,List<String>> map=soulService.findPaperContentByPaperId(id);
        return ResponseEntity.ok(map);
    }

    //分页查询试卷
    @GetMapping("/paper/findPaperByPage")
    public ResponseEntity findPaperByPage(@RequestParam(defaultValue = "1") Integer page,
                                          @RequestParam(defaultValue = "10") Integer pageSize){
        PageResult vo=soulService.findPaperByPage(page,pageSize);

        return ResponseEntity.ok(vo);

    }

    //根据id删除
    @DeleteMapping("/paper/delete")
    public ResponseEntity deletePaper(String id){
        try {

            soulService.deletePaper(id);
            return ResponseEntity.ok(true);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(false);
        }

    }

    //上传图片
    @PostMapping("/file/upload")
    public String upload(MultipartFile uploadFile) throws IOException {
        if(uploadFile!=null && uploadFile.getSize()>0){
            String url = ossTemplate.upload(uploadFile.getOriginalFilename(), uploadFile.getInputStream());
            System.out.println(url);
            return url;
        }
        return "";
    }

    //根据id查询试卷
    @GetMapping("/paper/findPaperById")
    public ResponseEntity findPaperById(String id){
        TestPaper paper=soulService.findPaperById(id);
        return ResponseEntity.ok(paper);

    }
    //保存或更新试卷
    @PostMapping("/paper/saveOrUpdatePaper")
    public ResponseEntity saveOrUpdatePaper(@RequestBody TestPaper papers,String paperId){
        try {

            if (papers.getId()==null){
                soulService.savePaper(papers);

            }else {
                soulService.updatePaper(papers,paperId);

            }
            return ResponseEntity.ok(true);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(false);
        }
    }
}
